/* Janet Putnam
CS 362
Assignment 4
Random Card Test - Test of council_room card

Include the following lines in make file:


randomtestcard:  randomtestcard.c dominion.o rngs.o
	gcc -o randomtestcard -g randomtestcard.c dominion.o rngs.o $(CFLAGS)
	
randomtestcard.out:  randomtestcard
	rm -f randomtestcard.out
	rm -f *.gcda *.gcno *.gcov
	./randomtestcardr 0 >> randomtestcard.out
	gcov -b dominion.c
	cat dominion.c.gcov >> randomtestcard.out

*/


#include "dominion/dominion.h"
#include "dominion/dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "dominion/rngs.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>


/* Copy of function from dominion.c for reference purposes, can be removed from code
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
...
//council room
int councilRoom_fcn(int currentPlayer, struct gameState *state, int handPos)
{
	int i;
	//+4 Cards
    for (i = 0; i < 4; i++)
	{
	  drawCard(currentPlayer, state);
	}
			
      //+1 Buy
      state->numBuys++;
			
      //Each other player draws a card
      for (i = 0; i < state->numPlayers; i++)
	{
	  if ( i != currentPlayer )
	    {
	      drawCard(i, state);
	    }
	}
			
      //put played card in played card pile
      discardCard(handPos, currentPlayer, state, 0);
			
      return 0;
}
*/

/*Note:  The council room card, player draws 4 cards, increases one buy, and all other players draw 1 card*/

int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus);
int councilRoom_fcn(int currentPlayer, struct gameState *state, int handPos);

int councilRoomTest (print_pass) {

	//set variables
	int result;
	int p0_hand;
	int p1_hand;
	int p2_hand;
	int p3_hand;
	int buys;
	int p0_deck;
	int p1_deck;
	int p2_deck;
	int p3_deck;
	int random;
	int loop_variable;

	//setup initial struct gameState
	struct gameState G;
	memset(&G, 0, sizeof(struct gameState));
	
	//select cards in game
	int k[10] = {adventurer, council_room, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

	for (loop_variable = 0; loop_variable < 2; loop_variable++)
	{
	
		//initialize gameState struct
		initializeGame(4, k, 4, &G);
		
		//set variable values for comparison
		p0_hand = G.handCount[0];
		p1_hand = G.handCount[1];
		p2_hand = G.handCount[2];
		p3_hand = G.handCount[3];
		buys = G.numBuys;
		p0_deck = G.deckCount[0];
		p1_deck = G.deckCount[1];
		p2_deck = G.deckCount[2];
		p3_deck = G.deckCount[3];

		//randomly pick player for turn
		random = (rand() % 4);
			
		//set players turn and give player council_room card
		G.whoseTurn = random;
		G.hand[random][0] = council_room;

		if(loop_variable == 0)
		{
		//execute card via cardEffect
		result = cardEffect(1, 0, 0, 0, &G, 0, 0);
		printf("Card called via cardEffect function.\n");
		printf("Random player = %d.\n", random);
		}
		else
		{
		//execute card via refactored function
		result = councilRoom_fcn(random, &G, 0);
		printf("Card called via refactored function.\n");
		printf("Random player = %d.\n", random);
		}
		
		//check that cardEffect ended w/ correct code
		if (result == 0)
		{
			if(print_pass == 0)
			{
				printf("PASS:  Function (council room) ended without error.\n");
			}
		}
		else
		{
			printf("FAIL:  Function (council room) end with error.\n");
		}
		
		//check that random player has 3 additional cards (+4 - 1 discard) & other players have drawn 1 additional card
		if (random == 0)
		{
			if (G.handCount[random] == p0_hand + 3)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards added correctly to selected player's hand.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not correctly added to selected player's hand.\n");
			}
				
			if (G.handCount[1] == p1_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (1 of 3).\n");
			}
				
			if (G.handCount[2] == p2_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (2 of 3).\n");
			}
				
			if (G.handCount[3] == p3_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (3 of 3).\n");
			}
		
			//verify that selected player's deck is reduced by 4 & other players' decks are each reduced by one
			if (G.deckCount[random] == p0_deck - 4)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards correctly removed from selected player's deck.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not correctly removed from selected player's deck.\n");
			}
				
			if (G.deckCount[1] == p1_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (1 of 3).\n");
			}
				
			if (G.deckCount[2] == p2_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (2 of 3).\n");
			}
				
			if (G.deckCount[3] == p3_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (3 of 3).\n");
			}
		}
		
		else if (random == 1)
		{
			if (G.handCount[random] == p1_hand + 3)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards added correctly to selected player's hand.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not added correctly to selected player's hand.\n");
			}
			
			if (G.handCount[0] == p0_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (1 of 3).\n");
			}
			
			if (G.handCount[2] == p2_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (2 of 3).\n");
			}
				
			if (G.handCount[3] == p3_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (3 of 3).\n");
			}
			
			//verify that selected player's deck is reduced by 4 & other players' decks are each reduced by one
			if (G.deckCount[random] == p1_deck - 4)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards correctly removed from selected player's deck.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not removed from selected player's deck.\n");
			}
			
			if (G.deckCount[0] == p0_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (1 of 3).\n");
			}
				
			if (G.deckCount[2] == p2_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (2 of 3).\n");
			}
				
			if (G.deckCount[3] == p3_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (3 of 3).\n");
			}
		}
			
		else if (random == 2)
		{
		
			if (G.handCount[random] == p2_hand + 3)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards added correctly to selected player's hand.\n");
				}
			}
			else
			{
				printf("FAI:  Four cards not added correctly to selected player's hand.\n");
			}
			
			if (G.handCount[0] == p0_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (1 of 3).\n");
			}
			
			if (G.handCount[1] == p1_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (2 of 3).\n");
			}
			
			if (G.handCount[3] == p3_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (3 of 3).\n");	
			}
			
			//verify that selected player's deck is reduced by 4 & other players' decks are each reduced by one
			if (G.deckCount[random] == p2_deck - 4)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards correctly removed from selected player's deck.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not correctly removed from selected player's deck.\n");
			}
			
			if (G.deckCount[0] == p0_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (1 of 3).\n");
			}
				
			if (G.deckCount[1] == p1_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (2 of 3).\n");
			}
				
			if (G.deckCount[3] == p3_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (3 of 3).\n");
			}
		}
			
		else if (random == 3)
		{
			if (G.handCount[random] == p3_hand + 3)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards added correctly to selected player's hand.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not added correctly to selected player's hand.\n");
			}	
			
			if (G.handCount[0] == p0_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (1 of 3).\n");
			}
			
			if (G.handCount[1] == p1_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (2 of 3).\n");
			}
			
			if (G.handCount[2] == p2_hand + 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card added correctly to other player's hand (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not added correctly to other player's hand (3 of 3).\n");
			}
			
			//verify that selected player's deck is reduced by 4 & other players' decks are each reduced by one
			if (G.deckCount[random] == p3_deck - 4)
			{
				if(print_pass == 0)
				{
					printf("PASS:  Four cards correctly removed from selected player's deck.\n");
				}
			}
			else
			{
				printf("FAIL:  Four cards not correctly removed from selected player's deck.\n");			
			}
			
			if (G.deckCount[0] == p0_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (1 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (1 of 3).\n");
			}
				
			if (G.deckCount[1] == p1_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (2 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (2 of 3).\n");
			}
				
			if (G.deckCount[2] == p2_deck - 1)
			{
				if(print_pass == 0)
				{
					printf("PASS:  One card correctly removed from other player's deck (3 of 3).\n");
				}
			}
			else
			{
				printf("FAIL:  One card not correctly removed from other player's deck (3 of 3).\n");
			}
			
		}
		else
		{
			printf("Error with random number generator.  Player generated is outside number of players.\n");
		}

		//check that buy increases by one
		if(G.numBuys == buys + 1)
		{
			if(print_pass == 0)
			{
				printf("PASS:  One buy correctly added.\n");
			}
		
		}
		else
		{
			printf("FAIL:  One buy not correctly added.\n");
		}
	}
	
	return 0;
	
}



int main(int argc, char **argv)
{	
	int print_pass;
	//if print_pass = 0, then results will print for passing tests.
	//if print_pass = 1, then results will not print for passing tests.
	//regardless of print_pass state, failing tests will print results.
	
	print_pass = atoi(argv[1]);
	if ((print_pass != 1 ) || (print_pass != 0))
	{
		print_pass = 0;
	}
	srand(time(NULL));
	councilRoomTest(print_pass);

	return 0;
}